package action

import (
	"gorm.io/gorm"

	"ssnotify/common/enums/notifysource"
	"ssnotify/common/enums/sendway"
	"ssnotify/common/utils"
	"ssnotify/internal/data/handler"
	"ssnotify/internal/notify/models"
)

type Handle struct {
	Orm *gorm.DB
}

func (action *Handle) Execute(topic string, jsonData []map[string]interface{}) {
	//遍历消息集合处理每个类型
	for _, notify := range jsonData {
		//获取当前的消息类中
		source := utils.GetStringValueFiled(notify, "source")
		//获取当前的类型
		switch source {
		case notifysource.Alarm.Get().Source:
			//告警类型的消息处理
			handleAlarmNotify(action.Orm, notify)
		case notifysource.UseOperate.Get().Source:
			//用户操作的消息处理
		case notifysource.SystemTask.Get().Source:
			//系统任务的消息处理
		}
	}
}

func ExecuteNotifyConfig(db *gorm.DB, notifyConfig models.NotifyConfig, notifyData map[string]interface{}) {
	notifyConfigModel := &models.NotifyConfig{}
	notifyConfigModel.Orm = db
	//查询通知配置对应的用户信息(告警通知有联系人配置)
	notifyUserList := notifyConfigModel.FindNotifyUserList(notifyConfig)
	//通知模板格式处理		{"1":"SMS_200710187","2":"65d32200-a776-441f-ad65-9745b094781c","3":"","4":"W4wHYmJ21KcXmKjtKPxquL_L-iCGCQohw2KJhW4WNuo"}
	templateConfig := utils.BytesToMap([]byte(notifyConfig.TemplateId))
	//遍历发送方式的逻辑处理
	for sendWay, templateId := range templateConfig {
		//获取当前的类型
		switch sendWay {
		//告警类型的消息处理
		case sendway.ShortMsg.Get().SendType:
			//短信通知
			handler.SendShortMsgNotify(db, templateId.(string), notifyData, notifyConfig, notifyUserList)
		case sendway.Email.Get().SendType:
			////邮件通知
			handler.SendEmailNotify(db, templateId.(string), notifyData, notifyConfig, notifyUserList)
		case sendway.System.Get().SendType:
			////应用内通知
			//handler.SendSystemNotify(notifyData, notifyConfig, notifyUserList)
		case sendway.WeChat.Get().SendType:
			////公众号通知
			//handler.SendWeixinNotify(notifyData, notifyConfig, notifyUserList)
		}
	}
}
